<?php
namespace Home\Controller;
use Think\Controller;
class CustomerController extends ZMController {
	public function customer(){
	    $custname=I('custname');
		$region=I('region');
		$like='';
		if(!empty($custname)){
			$like.="and (a.name like '%$custname%' or a.othername like '%$custname%')";
		}
		else{
			$like.="";
		}
		if($region!=''){
			$like.=" and a.region=$region";
		}
		else{
			$like.="";
		}
		$page=I('page');
		$page==''?$page=0:$page=$page;
		$limit=I('limit');
		$limit==''?$limit=20:$limit=$limit;
		$start=($page-1)*$limit;
		$limit=" limit $start,$limit";
		$Model=M();
		$count = $Model->query("select a.id from ddwk_customer as a where a.isdel<>1 and a.status<>4 and a.d1=1 $like");
		$res=$Model->query("select a.id,a.name,a.address,a.linkman,a.linktel from ddwk_customer as a where a.isdel<>1 and a.status<>4 and a.d1=1 $like $limit");
		$hospital=R('Cust/hospital');//延伸系统医院
		$hospital=i_array_column($hospital,'hid');
		foreach($res as &$row){
			$id=$row['id'];
			if(in_array($id,$hospital)){
				$row['booked']=1;
			}
			else{
				$row['booked']=0;
			}
		}
		$count=count($count);
		$this->ajax($res,$count);
    }
	public function selectEx(){//物业公司
		$hid=I('id');
		$page=I('page');
		$page==''?$page=0:$page=$page;
		$limit=I('limit');
		$limit==''?$limit=20:$limit=$limit;
		$start=($page-1)*$limit;
		$limit=" limit $start,$limit";
		$Model=M();
		$count=$Model->db(1,'DB_CONFIG1')->query("select id from ddwk_area where hid=$hid and station=1 and isdel=0");
		$res=$Model->query("select areaname,memo from ddwk_area where hid=$hid and station=1 and isdel=0 order by id $limit");
		$count=count($count);
		$this->ajax($res,$count);
	}
	public function selectArea(){//医护部门
		$hid=I('id');
		$page=I('page');
		$page==''?$page=0:$page=$page;
		$limit=I('limit');
		$limit==''?$limit=20:$limit=$limit;
		$start=($page-1)*$limit;
		$limit=" limit $start,$limit";
		$Model=M();
		$count=$Model->db(1,'DB_CONFIG1')->query("select id from ddwk_area where hid=$hid and station=2 and isdel=0");
		$res=$Model->query("select a.areaname,a.type,a.memo,b.name as point from ddwk_area as a left join ddwk_hcustomer as b on a.hcust=b.id where a.hid=$hid and a.station=2 and a.isdel=0 order by a.id $limit");
		$count=count($count);
		$this->ajax($res,$count);
	}
	public function selectS(){//收集人员
		$hid=I('id');
		$page=I('page');
		$page==''?$page=0:$page=$page;
		$limit=I('limit');
		$limit==''?$limit=20:$limit=$limit;
		$start=($page-1)*$limit;
		$limit=" limit $start,$limit";
		$Model=M();
		$count=$Model->db(1,'DB_CONFIG1')->query("select id from ddwk_employee where hid=$hid and station=1 and isdel=0");
		$res=$Model->query("select b.areaname,a.id as nfc1,a.name,a.memo,c.nfcname as nfc2 from ddwk_employee as a left join ddwk_area as b on a.area=b.id left join ddwk_nfc1 as c on a.nfcid=c.id where a.hid=$hid and a.station=1 and a.isdel=0 order by a.id $limit");
		$count=count($count);
		$this->ajax($res,$count);
	}
	public function selectE(){//审核人员
		$hid=I('id');
		$page=I('page');
		$page==''?$page=0:$page=$page;
		$limit=I('limit');
		$limit==''?$limit=20:$limit=$limit;
		$start=($page-1)*$limit;
		$limit=" limit $start,$limit";
		$Model=M();
		$count=$Model->db(1,'DB_CONFIG1')->query("select id from ddwk_employee where hid=$hid and station=2 and isdel=0");
		$res=$Model->query("select b.areaname,a.id as nfc1,a.name,a.memo,c.nfcname as nfc2 from ddwk_employee as a left join ddwk_area as b on a.area=b.id left join ddwk_nfc1 as c on a.nfcid=c.id where a.hid=$hid and a.station=2 and a.isdel=0 order by a.id $limit");
		$count=count($count);
		$this->ajax($res,$count);
	}
	public function selectH(){//产废点
		$hid=I('id');
		$page=I('page');
		$page==''?$page=0:$page=$page;
		$limit=I('limit');
		$limit==''?$limit=20:$limit=$limit;
		$start=($page-1)*$limit;
		$limit=" limit $start,$limit";
		$Model=M();
		$count=$Model->db(1,'DB_CONFIG1')->query("select id from ddwk_hcustomer where hid=$hid and isdel=0");
		$res=$Model->query("select a.id,a.name,a.address,b.nfcname,a.areaid,a.memo from ddwk_hcustomer as a left join ddwk_nfc2 as b on a.nfcid=b.id where a.hid=$hid and a.isdel=0 order by a.id $limit");
		foreach($res as &$row){
			$hcid=$row['id'];
			$areaid=$row['areaid'];
			$r1=$Model->query("select b.name from ddwk_hcustomer_master as a left join ddwk_employee as b on a.employeeid=b.id where a.hcid=$hcid");
			$desc = '';
            foreach($r1 as $row1){
                $desc.=','.$row1['name'];
            }
			$row['desc']=ltrim($desc,',');
			$areaid=explode(',',$areaid);
			$areadesc='';
			foreach($areaid as $row2){
				$r2=$Model->query("select areaname from ddwk_area where id=$row2 and isdel=0");
				$areadesc.=','.$r2['0']['areaname'];
			}
			$row['areadesc']=ltrim($areadesc,',');
		}
		$count=count($count);
		$this->ajax($res,$count);
	}
	public function customerCollect(){//卫生机构收集
		$custid=I('custid');
		$startdate=I('startdate');
		$enddate=I('enddate');
		if(empty($custid)||empty($startdate)||empty($enddate)){
			exit(0);
		}
		$res=$this->getCollect($startdate,$enddate,'',$custid);
		$this->ajax($res);
	}
	private function getCollect($startdate,$enddate,$region='',$custid='',$like=''){
		$Model=M();
	    $where='';
		$on1='';
		$on2='';
	    if(!empty($region)){
		    $where.=" and c.nid=$region";
	    }
	    if(!empty($custid)){
	    	$where.=" and a.id=$custid";
	    }
		if($startdate>='2017-10-01'){
			$startdate=$startdate;
			$on1.=" and b.pdate>='$startdate' and b.pdate<='$enddate'";
		}
		else{
			if($enddate<'2017-10-01'){
				$on2.=" and b.pdate>='$startdate' and b.pdate<='$enddate'";
			}
			else{
				$on1.=" and b.pdate>='2017-10-01' and b.pdate<='$enddate'";
				$on2.=" and b.pdate>='$startdate' and b.pdate<='2017-9-30'";
			}
		}
		$where.=$like;
		if(!empty($on1)){
		    $res=$Model->query("select b.garbage,b.lwq,b.guil,b.qt,a.name as custname,b.kg from ddwk_customer as a left join ddwk_collect as b on b.custid=a.id $on1 where 1=1 $where order by a.region,a.id");
		}
		if(!empty($on2)){
			$result=$Model->query("select b.garbage,b.oncething,b.otherthing,b.nocode,b.syd,b.qt,b.syp,b.lwq,b.zsq,b.guil,a.name as custname,b.kg from ddwk_customer as a left join ddwk_collect as b on b.custid=a.id $on2 where 1=1 $where order by a.region,a.id");
		}
		if(!empty($res)){
			for($i=0;$i<count($res);$i++){
				$arr=explode(',',$res[$i]['kg']);
				$res[$i]['totalweight']=$arr['0']/1000*$res[$i]['garbage'];
				$res[$i]['totalweight']+=$arr['1']/1000*$res[$i]['lwq'];
				$res[$i]['totalweight']+=$arr['2']/1000*$res[$i]['guil'];
				$res[$i]['totalweight']+=$arr['3']/1000*$res[$i]['qt'];
				$res[$i]['totalcollect']=$res[$i]['garbage']+$res[$i]['lwq']+$res[$i]['guil']+$res[$i]['qt'];
			}
		}
		if(!empty($result)){
			for($i=0;$i<count($result);$i++){
				$arr=explode(',',$result[$i]['kg']);
				$result[$i]['totalweight']=$arr['0']/1000*$result[$i]['garbage'];
				$result[$i]['totalweight']+=$arr['1']/1000*$result[$i]['oncething'];
				$result[$i]['totalweight']+=$arr['2']/1000*$result[$i]['nocode'];
				$result[$i]['totalweight']+=$arr['3']/1000*$result[$i]['guil'];
				$result[$i]['totalweight']+=$arr['4']/1000*$result[$i]['otherthing'];
				$result[$i]['totalweight']+=$arr['6']/1000*$result[$i]['lwq'];
				$result[$i]['totalweight']+=$arr['7']/1000*$result[$i]['zsq'];
				$result[$i]['totalweight']+=$arr['8']/1000*$result[$i]['syd'];
				$result[$i]['totalweight']+=$arr['9']/1000*$result[$i]['syp'];
				$result[$i]['totalweight']+=$arr['10']/1000*$result[$i]['qt'];
				$result[$i]['garbage']=$result[$i]['garbage']+$result[$i]['oncething']+$result[$i]['otherthing']+$result[$i]['nocode']+$result[$i]['syd']+$result[$i]['qt']+$result[$i]['syp'];
				$result[$i]['lwq']=$result[$i]['lwq']+$result[$i]['zsq'];
				$result[$i]['qt']=0;
				$result[$i]['totalcollect']=$result[$i]['garbage']+$result[$i]['lwq']+$result[$i]['guil']+$result[$i]['qt'];
				$res[]=$result[$i];
			}
		}
		$arr = array();
        foreach($res as $v){
            if(!isset($arr[$v['id']])){
			    $arr[$v['id']] = $v;
			}
            else {
				$arr[$v['id']]['garbage'] += $v['garbage'];
				$arr[$v['id']]['lwq'] += $v['lwq'];
				$arr[$v['id']]['guil'] += $v['guil'];
				$arr[$v['id']]['qt'] += $v['qt'];
				$arr[$v['id']]['totalweight'] += $v['totalweight'];
				$arr[$v['id']]['totalcollect'] += $v['totalcollect'];
			}
        }
	    return array_values($arr);
	}
}